從歷史來看,C++ 缺乏統一的硬體服務互動方式,迫使開發者陷入 「平台孤島」 其中程式碼庫因作業系統特定的 API(如 Win32 或 POSIX)而支離破碎。這張投影片標誌著一個轉折點,進入現代時代,C++ 標準程式庫成為通用的抽象層。
1. 終結 #ifdef 調理盤式程式碼
在標準化之前,像啟動執行緒或瀏覽目錄等簡單任務,需要使用預處理器巨集來處理不同的系統標頭(例如, <windows.h> 對比 <pthread.h>)。這導致了臃腫且難以維護的程式碼。
2. C++11 的范式轉變
標準開始重新掌握系統資源的控制權。特別是, C++11 加入了高階併發功能,包括 std::thread、std::mutex 及 std::future,從而標準化了語言與中央處理器之間的關係。
3. 解耦供應商邏輯
透過超越平台特定程式碼,標準程式庫提供了「寫一次,編譯到任何地方」的保證。平台維護的負擔從開發者轉移至編譯器供應商。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which C++ standard first introduced a unified concurrency model into the Standard Library?
C++98
C++11
C++14
C++17
✅ Correct!
C++11 added std::thread, std::mutex, and the memory model required for standardized concurrency.❌ Incorrect
Prior to C++11, concurrency was handled by external libraries or platform APIs like POSIX.QUESTION 2
What is the primary disadvantage of using '#ifdef _WIN32' for threading logic?
It makes code non-portable and difficult to maintain.
It runs slower than std::thread.
It prevents the use of templates.
It requires a specific commercial license.
✅ Correct!
Platform-specific macros create 'spaghetti' code that must be rewritten for every new operating system.❌ Incorrect
While it might be fast, the main issue is the lack of portability and the fragmented logic.QUESTION 3
Which utility added in C++11 is used to manage a value that will be available later?
std::vector
std::mutex
std::future
std::chrono
✅ Correct!
std::future provides a mechanism to access the result of an asynchronous operation.❌ Incorrect
std::mutex is for mutual exclusion; std::future is for asynchronous results.QUESTION 4
Who is responsible for implementing the OS-specific logic behind 'std::thread'?
The application programmer
The hardware manufacturer
The compiler/standard library vendor
The user at runtime
✅ Correct!
The compiler vendor (like GCC, Clang, or MSVC) maps standard calls to the target OS kernel calls.❌ Incorrect
The abstraction layer's purpose is to remove this responsibility from the application developer.QUESTION 5
What does the term 'Platform Silo' refer to in this context?
A secure storage area for source code.
Code that can only run on one specific OS due to API dependencies.
A high-performance server farm.
A design pattern for database isolation.
✅ Correct!
Silos occur when code is locked into proprietary APIs (e.g., using Win32 API calls directly).❌ Incorrect
It refers to architectural isolation and lack of portability, not literal storage.Case Study: Modernizing a Legacy Downloader
Transitioning from POSIX to Modern C++ Standard Library
A developer in 2005 wrote a multi-threaded downloader using 'CreateThread' for Windows and 'pthread_create' for Linux. The codebase is 40% preprocessor macros.
Q
1. What single C++11 class should replace both CreateThread and pthread_create?
Solution:
The developer should use
The developer should use
std::thread. This standardizes the execution model and removes the need for platform-specific headers.Q
2. If the downloader needs to return the size of the downloaded file asynchronously, which concurrency feature is most appropriate?
Solution:
std::future combined with std::async (or a promise) is best for retrieving return values from background tasks safely and cleanly.Q
3. What is the 'Write Once, Compile Anywhere' benefit in this scenario?
Solution:
It means the same threading source code can be compiled for Windows, Linux, or macOS without any changes, as the Standard Library handles the OS translation.
It means the same threading source code can be compiled for Windows, Linux, or macOS without any changes, as the Standard Library handles the OS translation.